#include "dingdandetailwidget.h"
#include "ui_dingdandetailwidget.h"

DingdanDetailWidget::DingdanDetailWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DingdanDetailWidget)
{
    ui->setupUi(this);

    setWindowTitle("订单详情");

    model = new SaleModel();
    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->dateEdit->setDate(QDate::currentDate());
    ui->dateEdit->setDisplayFormat("yyyy-MM-dd");

    connect(&dingdanService, &DingdanService::getDingdanInfoSuccess,
            [=](Dingdan *dingdan)
    {
        qDebug()<<"A";
        this->dingdan = dingdan;
        this->initValue();
    });
}

DingdanDetailWidget::~DingdanDetailWidget()
{
    delete ui;
}

void DingdanDetailWidget::setSelectionRow(){
    model->appendRow(*(new QList<QStandardItem*>));
    ui->tableView->setSpan(model->rowCount()-1, 0, 1, model->columnCount());
}

void DingdanDetailWidget::showWithDingdanId(QString dingdanId){
    model->reset();
    setSelectionRow();
    dingdanService.getDingdanDetails(this, dingdanId);
    exec();
}

void DingdanDetailWidget::initValue(){
    qDebug()<<"B";
    UserRelation *relation = dingdan->getUserRelation();
    qDebug()<<(relation==nullptr);
    qDebug()<<(relation->contact==nullptr);
    qDebug()<<"B1";
    ui->lineEditContact->setText(relation==nullptr?"":relation->contact);
    qDebug()<<"B2";
    ui->lineEditNickName->setText(relation==nullptr?"":relation->nickName);
    qDebug()<<"B3";
    ui->lineEditTel->setText(relation==nullptr?"":relation->tel);
    qDebug()<<"C";
    ui->lineEditDingdanId->setText(dingdan->dingdanId);
    ui->comboBox->setEnabled(false);
    ui->comboBox->setCurrentIndex(dingdan->isTicket);
    ui->dateEdit->setDate(QDate::fromString(dingdan->date, "yyyy-MM-dd"));
    ui->lineEditDingdanRemark->setText(dingdan->remark);

    model->insertDingdanDetail(dingdan->details);

    ui->labelTypeName->setText(dingdan->getTypeStr());
    ui->labelSourceFeightPaid->setText("我方已付："+QString::number(dingdan->sourceFreightPaid, 'f', 2));
    ui->labelSourceFeightUnPaid->setText("我方未付："+QString::number(dingdan->sourceFreightUnpaid, 'f', 2));
    ui->labelTargetFeightPaid->setText("对方垫付："+QString::number(dingdan->targetFreightPaid, 'f', 2));
    ui->labelTotalCount->setText(QString("数量：%1").arg(dingdan->totalCount));
    ui->labelTotalMoney->setText("金额："+QString::number(dingdan->totalMoney, 'f', 2));
    ui->labelProfit->setText("利润："+QString::number(dingdan->profit, 'f', 2));
    if(dingdan->type == Dingdan::SaleOut || dingdan->type == Dingdan::SaleBack){
        ui->labelDingdanTicket->setText("应开发票："+QString::number(dingdan->ticketOut, 'f', 2));
        ui->labelDingdanMoney->setText("应收货款："+QString::number(dingdan->moneyIn, 'f', 2));
        ui->labelFreeTicket->setText("免开票："+QString::number(dingdan->freeTicket, 'f', 2));
        ui->labelFreeMoney->setText("免收款："+QString::number(dingdan->freeMoney, 'f', 2));
        ui->labelTicket->setText("开票："+QString::number(dingdan->ticket, 'f', 2));
        ui->labelMoney->setText("收款："+QString::number(dingdan->money, 'f', 2));
    }else{
        ui->labelDingdanTicket->setText("应收发票："+QString::number(dingdan->ticketOut, 'f', 2));
        ui->labelDingdanMoney->setText("应付货款："+QString::number(dingdan->moneyIn, 'f', 2));
        ui->labelFreeTicket->setText("免收票："+QString::number(dingdan->freeTicket, 'f', 2));
        ui->labelFreeMoney->setText("免付款："+QString::number(dingdan->freeMoney, 'f', 2));
        ui->labelTicket->setText("收票："+QString::number(dingdan->ticket, 'f', 2));
        ui->labelMoney->setText("付款："+QString::number(dingdan->money, 'f', 2));
    }
}
